<?php
include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/model/nms_interface_model.php");
include_once(dirname(__FILE__) . "/libs/config.php");
function draw_interface_manage(){
	html_master_script_filter();
	html_master_script_checkall();
	html_interface_script_reload();
	html_interface_manage_script();
	draw_interface_manage_header();
		if (empty($_GET['host_id'])) {
			$id_host="";
		}
		else{
			$id_host =  $_GET['host_id'];
		}
		if (empty($_GET['alias'])) {
			$alias="name";
		}
		else{
			$alias =  $_GET['alias'];
		}
		if (empty($_GET['search'])) {
			$id_search="";
		}
		else{
			$id_search =  $_GET['search'];
		}
		$sql_where  ="";
		if (!empty($_GET['host_id'])) {
			if($_GET['host_id']=='null'){
				$sql_where.="";
			}else{
				$sql_where .=" and a.host_id =".$_GET['host_id']."";
			}
		}
		if (!empty($_GET['alias'])) {
			if($_GET['alias']=='name'){
				$sql_where.=" and a.interface_alias !=''";
			}
			if($_GET['alias']=='all'){
				$sql_where.="";
			}
		}
		if (!empty($_REQUEST['search'])) {
			$research=trim($_REQUEST['search']);
			$sql_where.=" and (CONCAT('(',a.interface_name,')','\t',a.interface_alias) like '%".$research."%')";
		}else{
			$_REQUEST['search']="";
		}
		if(($_REQUEST['tab']=="group_interface")  and empty($_REQUEST['alias']) and empty($_REQUEST['host_id']) and empty($_REQUEST['search']) )
		{
			$sql_where.="and a.host_id ='1'";
		}
		$query = "SELECT count(a.interface_id)
					FROM plugin_nms_interface a, host c
					where a.host_id = c.id ".$sql_where."";
		$display =db_fetch_cell($query);
		$querydata="SELECT a.interface_id as id, CONCAT_WS('',c.description,' (',c.hostname,')') as host_name,CONCAT('(',a.interface_name,')','\t',a.interface_alias) as interface_des ,b.group_name as group_interface
  					FROM plugin_nms_interface a , host c , plugin_nms_interfacegroup d,plugin_nms_groupname b
					where  a.host_id = c.id and a.interface_id = d.interface_id and d.interface_group_id = b.interface_group_id  ".$sql_where."
							order by Convert(SUBSTRING(a.interface_name,3,1),UNSIGNED INTEGER),Convert(SUBSTRING(a.interface_name,5,2),UNSIGNED INTEGER),a.interface_alias DESC limit 0,$display";
		$url_post= "nms.php?run=group&tab=group_interface&host_id=".$id_host."&alias=".$alias."&search=".$id_search."";
		?>
		<form action="<?php echo $url_post ?>" method ="post"enctype="multipart/form-data" id ="id_check_table_all">
		<?php
			draw_interface_manage_table($querydata);
			    if (isset($_GET['search'])) {
		?>
				<script >
					var tmp = $('#id_master_table tr').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
					alert(test);
					$('#id_master_table').empty();
					$('#id_master_table').html(test);
				</script>
		<?php 
			}
		 	draw_interface_manage_footer();
		  	if (empty($_GET['host_id'])) {
		    	$host_id="";
		   	}
		  	else{
		    	$host_id =  $_GET['host_id'];
		  	}
		  	if (empty($_GET['alias'])) {
		  		$alias="name";
		  	}
		  	else{
		  		$alias =  $_GET['alias'];
		  	}
		  	
	?>
   		 <script type="text/javascript">
				    	$("#id_devices").val('<?php echo $host_id;?>');
				    	$("#id_alias").val('<?php echo $alias;?>');
		 </script>
 	<?php 
 	if(isset($_POST['save']))
 	{
 	
 		if(!empty($_POST['inter'])){
 			$inter = $_POST['inter'];
 			array_map('intval',$inter);
 			$inter = implode(',',$inter);
 			$inter = explode(",", $inter);
 			$string ="";
 			if(empty($_POST['id_group'])){
 				echo'
							<script>
									alert("Group_interface not null!");
							</script>';
 			}else{
 				for($x=0;$x<count($inter);$x++){
 					$interface_id=(int)$inter[$x];
 					 
 					delete_interface_to_group($interface_id);
 					interface_to_group($_POST['id_group'],$interface_id);
 					 
 				}
 				echo'
						<script>
								alert("		Successful !!!");
						</script>';
 			}
 		}else{
 				echo'
							<script>
									alert("Please choose interface !!");
							</script>';
 		}
 		echo '<script>
		 			reLoad1();
		 		</script>';
 	}
	}
 ?>
 <?php
 	function draw_interface_manage_header(){
		html_master_header_start("Interface Management");
		
 		?>
    	<table width="50%" border="0">
			    	    <tr>
			    	    <td  >Alias:</td>
					    		<td>
					    			<select  onchange="val()" id="id_alias">
					    				<option value="name" selected="selected">Used</option>
					    				<option value="all">All</option>
					        		</select>
					    		</td>
			    		<td  >Devices:</td>
					    		<td>
					    			<select  onchange="val()" id="id_devices">
					    				<option value="">Any</option>
						        		<?php
				     					 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");      $result3 = db_fetch_assoc($sql54);
				     					 if (sizeof($hosts) > 0) {
									     foreach($hosts as $set) {
									     echo "<option value='" . $set["id"] . "'"; 
									     if(isset($_REQUEST["host_id"])){if($_REQUEST["host_id"] == $set["id"]){echo "selected";}};
									     echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
									     }
									     }
				   						?>
					        		</select>
					    </td>
					    <script type="text/javascript">
						$("#id_devices").chosen({
							search_contains: true,
							no_results_text:"Oops, nothing found!"});
						$("#id_devices").on('change',function(){
						 	//get current selected value of combobox
							var devices = $(this).val();
						 	 var searchFilter = $("#id_search").val();
						 	var alias = $("#id_alias").val(); 	 
						 	//build query string
						 	var queryString = "run=group&tab=group_interface&host_id="+devices+"&alias="+alias+"&search="+searchFilter;
						 	document.location.href = "?"+queryString;
						});
 					 </script>
					     <td>Search</td>
							<td colspan="3" scope="col">
	    					<input id="id_search" type="search" onkeypress="keydown(event)"/>
	    			</td>
					</tr>
		</table>
 		<?php 
 		html_master_header_end();
 	} 
 	function  draw_interface_manage_table($querydata){
		html_master_table_start("100%", "center")
	?>
						<th width="3%" scope="col">No.</th>
						<th width="40%" scope="col">Interface</th>
						<th width="20%" scope="col" >Group Name</th>
						<th width="1%" scope="col" >
						 <input type="checkbox" name='checkall' id='nms-checkbox-checkall' onclick='checkedAll("id_check_table_all");'/>
						</th>
					</tr>
					<?php 
					$result_interface = db_fetch_assoc($querydata);
					$stt=1;
					foreach ($result_interface as $set) {
					?>
					<tr <?php if($stt%2==0) {echo 'data-status ="1"class="xanhlot"';}
				 					else {echo 'data-status ="2"class="xanhdam"';}?> > 
					 	<td align="center">&nbsp;<?php echo $stt++  ?></td>
						<td align="center"><?php echo $set['interface_des'] ?></td>
						<td align="center"> <?php echo $set['group_interface'] ?></td>
						<td align="center">&nbsp;<input type="checkbox" value="<? echo $set['id']; ?>"id="inter[]" name="inter[]" class='cls-alarm-table-checkbox'/>
						</td>
					</tr>
					<?php 
					}
					?>
	<?php 
		html_master_table_end();
 	}	
 	function draw_interface_manage_footer(){
		html_master_footer_start();
 	?>
		<div id="id_master_header">
		</div>
		<div id="id_master_center_top">
			<input class="action_pause"  type = "submit" name = "save" value="Go">
				<select   class="action_cancel"  onchange="val()" name="id_group">
				    				<option value=""></option>
					        		<?php
			     					 $hosts = db_fetch_assoc("select interface_group_id,group_name from plugin_nms_groupname order by group_name");     
			     					 if (sizeof($hosts) > 0) {
								     foreach($hosts as $set) {
								     echo "<option value='" . $set["interface_group_id"] . "'"; echo ">" . htmlspecialchars($set["group_name"]) . "</option>\n";
								     }
								     }
			   						?>
				</select>
			<h4 class="action" >Group Interface:</h4>
			<div class="countdown_time"  >
				  	 	 				<h4><div id="Timerefresh">Refresh time: </div></h4>
				  	 	 				<h4><div id ="countdown_time2"></div></h4>
				  	 	 			</div>
				  	 	 		</div>
<?php
		 html_alarm_script_time();
 		 html_master_footer_end();
 	}
 	
  ?>